18. Working with JSON

Working with JSON and XML

In this section, you will learn how to:

  • Compare the JSON and XML formats
  • Serialize and deserialize Java objects to/from JSON using the Jackson library.
  • Compare the JSON and XML formats
  • Name JAXB as an XML serialization library

ND079 JPND C2 L02 A18 JSON And XML

What are JSON and XML?

JSON (JavaScript Object Notation) and XML (Extensible Markup Language) are two common text formats for serializing data.

JSON Example

Here's one way we could represent client data using JSON:

{
  "id": 17,
  "name": "George Washington",
  "emails": ["george.w@gmail.com", "potus.ftw@yahoo.com"]
}
  • A curly bracket ({) denotes the start of an object.
  • Each object contains a set of property names and values separated by the colon (:) character.
  • Property names are surrounded with double-quotes (").
  • Values are allowed to be numbers, booleans, strings, lists, or nested JSON objects.
  • There is a trailing comma after each value, except for the last value in an object.

XML Example

Here's how you might do it in XML:

<?xml version="1.0" encoding="UTF-8" ?>
<client>
  <id> 17 </id>
  <name> George Washington </name>
  <emails>
    <email> george.w@gmail.com </email>
    <email> potus.ftw@yahoo.com </email>
  </emails>
</client>
  • Just like HTML, XML has angle bracket tags that need to be closed with a forward-slash (< />)
  • Each property gets its own tag, and all the properties are wrapped inside one big tag.
  • For list properties, XML uses a bunch of tags with the same name.

QUIZ QUESTION::

Match the representation with the format it corresponds to.

ANSWER CHOICES:



Example

Format

{
    "id" : "122131",
    "items" : [
           {"name": "Diet coke"},
           {"name": "Snapple"} 
     ] 
}
<order>
   <id>122131</id>
   <items>
      <item><name>Diet coke</name></item>
      <item><name>Snapple</name></item>
   </items>
</order>
SOLUTION:

Example

Format

<order>
   <id>122131</id>
   <items>
      <item><name>Diet coke</name></item>
      <item><name>Snapple</name></item>
   </items>
</order>
{
    "id" : "122131",
    "items" : [
           {"name": "Diet coke"},
           {"name": "Snapple"} 
     ] 
}

JSON Serialization / Deserialization with Jackson

In Java, the most popular library for doing this is called Jackson. Jackson uses Java annotations to help it get the metadata needed for serialization and deserialization.

To serialize the annotated Java object, you call the ObjectMapper.writeObject() method, passing in the annotated Java object. Jackson will convert the object to a JSON string.

To deserialize the JSON string, pass it into the ObjectMapper.readValue() method. This method also has an argument for the type of Java class that should be created. Jackson will create an instance of that class and fill it in using the information from the JSON string.

XML Serialization / Deserialization with JAXB

Just like Jackson, JAXB uses Java annotations to tell it how to serialize and deserialize Java objects. The only difference is the format of the output — XML in this case instead of JSON — and the methods used to do the conversions.

In JAXB, you use the Marshaller.marshal method to serialize to XML, and you use the Unmarshaller.unmarshal method to deserialize back into a Java class.

By the way "marshal" is another way of saying "serialize".